package org.hry.socket.bio.longlink;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class HeadFlagSocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(8888);
        Socket socket = server.accept();
        InputStream in = socket.getInputStream();
        while (true){
            byte[] bytes = new byte[4];
            int read = in.read(bytes);
            if (read  != 0){
                byte[] msgBytes = new byte[Integer.parseInt(new String(bytes))];
                in.read(msgBytes);
                String s = new String(msgBytes, Charset.defaultCharset());
                // 接收到信息，拿到接收的信息之后就可以处理了。
                System.out.println("接收到消息：" + s);
            }
        }

    }
}
